import React from 'react';
import { message } from 'antd';
import { superviseAlert } from 'Constants/alertMenuConfig';
import Api from 'Utils/api';
import { BUSINESS_INF0 } from 'Constants/config';
import Tab from './../../../TabChange';
import CommonBorder from './../../DispatchPage/Components/CommonBorder';
import './index.scss';


let VideoIndex = 0;
class OneKeyPatrol extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      data: [],
    };
    this.initData = ::this.initData;
    this.setInit = ::this.setInit;
    this.playing = ::this.playing;
  }
  componentWillUnmount() {
    preview.StopPreview(-1);
    clearInterval(this.timer);
    this.timer = undefined;
  }
  componentDidMount() {
    document.getElementById('ocx-parent').innerHTML = `
    <object classid="clsid:BE020CC9-521F-4641-85E1-3B631B7ADDD9" id="preview" width="100%" height="100%"  name="preview" >
       <PARAM NAME="Ip" VALUE="60.12.65.115"/>
       <PARAM  NAME="UserName " VALUE="admin"/>
       <PARAM  NAME="PrivilegeCode" VALUE="0401,0402,"/>
    </object> `;
    this.initData();
    //this.setInit();
  }
  initData() {
    const api = new Api({
      method: 'GET',
      path: '/warning/camera/code',
      success: (res) => {
        this.setState({
          data: res
        }, () => {
          if (res && res.length > 0) {
            this.setInit();
          } else {
            message.config({
              top: 200,
            });
            message.info('暂无可轮询企业！');
          }
        });
      },
      error: (msg) => {
        message.error(msg);
      }
    });
    api.run();
  }
  setInit() {
   // 判断海康是否已经登录
    const param = `
    <?xml version="1.0" encoding="utf-8"?><LoginInfo><LoginType>2</LoginType><SynLogin>1</SynLogin><IP>${BUSINESS_INF0.ServerIP}</IP><Port>${BUSINESS_INF0.Port}</Port><UserName>${BUSINESS_INF0.UserName}</UserName><Password>${BUSINESS_INF0.PWD}</Password></LoginInfo>`;
    const res = preview.LoginPlat(param);
    preview.SetLayoutType(1); // 设置窗格数
    if (res !== 0) {
      console.log('登录海康失败');
    } else {
      this.handlePlay();
    }
  }

  handlePlay() {
    const { data } = this.state;
    this.playing(data[VideoIndex]);
    this.timer = setInterval(() => {
      this.playing(data[VideoIndex]);
      VideoIndex = VideoIndex + 1 > data.length - 1 ? 0 : VideoIndex + 1;
    }, 20000);
  }
  playing(camera) {
    const _param = `<?xml version="1.0" encoding="UTF-8"?><Preview><CamIndexCode>${camera}</CamIndexCode></Preview>`;
    preview.StartPreview(_param);
  }
  render() {
    return (
      <div>
        <Tab
          configTab={superviseAlert}
        />
        <CommonBorder
          content={
            <div className="patrol-wrap" id="ocx-parent" />
          }
        />
      </div>
    );
  }
}

export default OneKeyPatrol;
